package net.homelinux.chaoswg.io.renga.core.registries;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class GlobalPropertyRegistry {
    private static Map<String, Object> _mapping = null;
    private static GlobalPropertyRegistry _this = null;
    private static List<PropertyChangeListener> listeners = null;
    
    private GlobalPropertyRegistry () {
        listeners = new Vector<PropertyChangeListener>();
        _mapping = new HashMap<String, Object>();
    }
    
    public static  GlobalPropertyRegistry getGlobalPropertyRegistry () {
        if (_this == null) {
            _this = new GlobalPropertyRegistry ();
        }
        return _this;
    }
    
    public void setProperty (final String propertyName, final Object property) {
        final Object oldValue = _mapping.get(propertyName);
        if (! property.equals(oldValue)) {
            _mapping.put(propertyName, property);
            firePropertyChanged(propertyName, oldValue, property);
        }
    }
    
    public Object getProperty (final String propertyName) {
        return _mapping.get(propertyName);
    }
    
    public void addPropertyChangeListener (final PropertyChangeListener pcl) {
        listeners.add(pcl);
    }
    
    public void removePropertyChangeListener (final PropertyChangeListener pcl) {
        listeners.remove(pcl);
    }
    
    private void firePropertyChanged (String propertyName, Object oldValue, Object newValue) {
        for(final PropertyChangeListener pcl : listeners) {
            pcl.propertyChange(new PropertyChangeEvent (this, propertyName, oldValue, newValue));
        }
    }
}
